package com.ws.ssmzongjie3.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.ssmzongjie3.bean.Acl_User;
import com.ws.ssmzongjie3.bean.Column;
import com.ws.ssmzongjie3.bean.ProVo;
import com.ws.ssmzongjie3.bean.Route;
import com.ws.ssmzongjie3.service.ColumnService;
import com.ws.ssmzongjie3.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/column")
public class ColumnController {
    @Autowired
    private ColumnService columnService;
    @Autowired
    private RouteService routeService;
    //栏目
    @RequestMapping("/list")
    
    public String select(Model model, ProVo proVo, Column column){
        System.out.println("column:"+column);

        PageHelper pageHelper = new PageHelper();//分页帮助类
        pageHelper.startPage(proVo.getCurrentPage(),proVo.getPageSize());//分页   每页条数
        List<Column> columns = columnService.listColumn(column);
        PageInfo pageInfo = new PageInfo(columns);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("user",column);

        return "/WEB-INF/admin/column/list";
    }

    @RequestMapping("/add")
    public String add(Model model, Column column){
        model.addAttribute("column",column);
        return "/WEB-INF/admin/column/add";
    }

    @RequestMapping("/insert")
    public String insert(Model model, Column column){
        columnService.insertColumn(column);
        return "redirect:/column/list";
    }

    @RequestMapping("/huixian")
    public String edit(Model model, Column column){
        Column column1 = columnService.getColumnById(column);
        model.addAttribute("column",column1);
        return "/WEB-INF/admin/column/update";
    }
    @RequestMapping("/update")
    public String update(Model model, Column column){
        columnService.updateColumn(column);
        return "redirect:/column/list";
    }
    @RequestMapping("/delete")
    public String delete(Model model, Column column){
        Route route = new Route();
        route.setColumn_id(column.getColumn_id());

        List<Route> routes = routeService.listRoute(route);
        if (routes.size()==0){
            columnService.deleteColumn(column);
        }
        return "redirect:/column/list";
    }
}
